<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            var maxProfit = function (k, prices) {
                const dp = new Array(prices.length).fill().map(() => new Array(2 * k + 1).fill(0))
                for (let j = 1; j < 2 * k + 1; j += 2) {
                    dp[0][j] = -prices[0]
                }
                for (let i = 1; i < prices.length; i++) {
                    for (let j = 0; j < 2 * k - 1; j += 2) {
                        dp[i][j + 1] = Math.max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i])
                        dp[i][j + 2] = Math.max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i])
                    }
                }
                return dp[dp.length - 1][2 * k]
            }
        </script>
    </body>
</html>
